/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PengajarUpdateDialog.java
 *
 * Created on 27 Agu 14, 18:59:19
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.PengajarDAO;
import com.ivanbiz.dao.impl.PengajarDAOImpl;
import com.ivanbiz.model.Pengajar;
import com.ivanbiz.service.JTextFieldLimit;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author Shbt Peterpan
 */
public class PengajarUpdateDialog extends JDialog {

    Pengajar pengajar;
    List<Pengajar> listPengajar;
    PengajarDAO pengajarDAO = new PengajarDAOImpl();

    /**
     * Creates new form PengajarUpdateDialog
     *
     * @param mainFrame
     * @param modal
     */
    public PengajarUpdateDialog(MainFrame mainFrame, boolean modal) {
        super(mainFrame, modal);
        initComponents();
        jLabel5.setVisible(false);
        textTelp.setVisible(false);
        jLabel8.setVisible(false);
        textTanggal.setVisible(false);
    }

    public PengajarUpdateDialog(MainFrame mainFrame, boolean modal, Pengajar pengajar) {
        super(mainFrame, modal);
        initComponents();
        this.pengajar = pengajar;
        labelPengajar.setText("Ubah Pengajar");
        textNIP.setText(pengajar.getNIP());
        textNama.setText(pengajar.getNama());
        textTelp.setText(pengajar.getTelp());
        textHP.setText(pengajar.getHandphone());
        textEmail.setText(pengajar.getEmail());
        textAlamat.setText(pengajar.getAlamat());
        textTanggal.setDate(pengajar.getDate());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelPengajar = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        buttonSimpan = new javax.swing.JButton();
        buttonBatal = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        textNIP = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        textNama = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        textTelp = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        textHP = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        textEmail = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        textTanggal = new com.toedter.calendar.JDateChooser();
        jScrollPane2 = new javax.swing.JScrollPane();
        textAlamat = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setModal(true);
        setResizable(false);

        labelPengajar.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        labelPengajar.setText("Tambah Pengajar Baru");

        buttonSimpan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/simpan.jpg"))); // NOI18N
        buttonSimpan.setText("Simpan");
        buttonSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSimpanActionPerformed(evt);
            }
        });
        jPanel3.add(buttonSimpan);

        buttonBatal.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/batal.jpg"))); // NOI18N
        buttonBatal.setText("Batal");
        buttonBatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBatalActionPerformed(evt);
            }
        });
        jPanel3.add(buttonBatal);

        jPanel4.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel2.setText("NPWP :");

        textNIP.setDocument(new JTextFieldLimit(10));

        jLabel4.setText("Nama :");

        textNama.setDocument(new JTextFieldLimit(30));

        jLabel5.setText("Telp :");

        textTelp.setDocument(new JTextFieldLimit(20));

        jLabel6.setText("Handphone :");

        textHP.setDocument(new JTextFieldLimit(20));

        jLabel7.setText("Email :");

        textEmail.setDocument(new JTextFieldLimit(50));

        jLabel8.setText("Tanggal Lahir :");

        jLabel9.setText("Alamat :");

        textTanggal.setDateFormatString("dd MMMM yyyy");

        textAlamat.setDocument(new JTextFieldLimit(100));
        textAlamat.setRows(5);
        jScrollPane2.setViewportView(textAlamat);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textNIP, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jLabel2)
                    .addComponent(textNama, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jLabel4)
                    .addComponent(textTelp, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jLabel5)
                    .addComponent(textHP, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jLabel6)
                    .addComponent(textEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(textTanggal, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textNIP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textTelp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textHP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textTanggal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelPengajar, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelPengajar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        setSize(new java.awt.Dimension(416, 565));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSimpanActionPerformed
        if (pengajar == null) {
            pengajar = new Pengajar();
        }
        pengajar.setNIP(textNIP.getText());
        pengajar.setNama(textNama.getText());
        pengajar.setTelp(textTelp.getText());
        pengajar.setHandphone(textHP.getText());
        pengajar.setEmail(textEmail.getText());
        pengajar.setDate(textTanggal.getDate() == null ? new Date() : textTanggal.getDate());
        pengajar.setAlamat(textAlamat.getText());
        validate(pengajar);
}//GEN-LAST:event_buttonSimpanActionPerformed

    private void buttonBatalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBatalActionPerformed
        dispose();
}//GEN-LAST:event_buttonBatalActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBatal;
    private javax.swing.JButton buttonSimpan;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel labelPengajar;
    private javax.swing.JTextArea textAlamat;
    private javax.swing.JTextField textEmail;
    private javax.swing.JTextField textHP;
    private javax.swing.JTextField textNIP;
    private javax.swing.JTextField textNama;
    private com.toedter.calendar.JDateChooser textTanggal;
    private javax.swing.JTextField textTelp;
    // End of variables declaration//GEN-END:variables

    private void validate(Pengajar pengajar) {
        if (pengajar == null) {
            JOptionPane.showMessageDialog(this, "Pengajar tidak boleh null");
//        } else if (pengajar.getNIP() == null) {
//            JOptionPane.showMessageDialog(this, "NIP tidak boleh null");
//        } else if (pengajar.getNIP().trim().isEmpty()) {
//            JOptionPane.showMessageDialog(this, "NIP tidak boleh kosong");
//        } else if (pengajar.getNama() == null) {
//            JOptionPane.showMessageDialog(this, "Nama tidak boleh null");
//        } else if (pengajar.getNama().trim().isEmpty()) {
//            JOptionPane.showMessageDialog(this, "Nama tidak boleh kosong");
//        } else if (pengajar.getTelp() == null) {
//            JOptionPane.showMessageDialog(this, "Telp tidak boleh null");
//        } else if (pengajar.getTelp().trim().isEmpty()) {
//            JOptionPane.showMessageDialog(this, "Telp tidak boleh kosong");
//        } else if (pengajar.getHandphone() == null) {
//            JOptionPane.showMessageDialog(this, "HP tidak boleh null");
//        } else if (pengajar.getHandphone().trim().isEmpty()) {
//            JOptionPane.showMessageDialog(this, "HP tidak boleh kosong");
//        } else if (pengajar.getEmail() == null) {
//            JOptionPane.showMessageDialog(this, "Email tidak boleh null");
//        } else if (pengajar.getEmail().trim().isEmpty()) {
//            JOptionPane.showMessageDialog(this, "Email tidak boleh kosong");
//        } else if (new ServiceHelper().validateEmail(pengajar.getEmail())) {
//            JOptionPane.showMessageDialog(this, "Format Email salah");
//        } else if (pengajar.getDate() == null) {
//            JOptionPane.showMessageDialog(this, "Date tidak boleh null");
//        } else if (pengajar.getAlamat() == null) {
//            JOptionPane.showMessageDialog(this, "Alamat tidak boleh null");
//        } else if (pengajar.getAlamat().trim().isEmpty()) {
//            JOptionPane.showMessageDialog(this, "Alamat tidak boleh kosong");
        } else {
            try {
                pengajarDAO.saveOrUpdate(pengajar);
                dispose();
            } catch (Exception ex) {
                Logger.getLogger(PengajarUpdateDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
